<template>
  <div>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :defaults.sync="defaults"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="$refs.workStationExportComp.visible = true">导出</el-button>
      </template>
    </avue-crud>
    <export-comp ref="workStationExportComp" page-api-url="mes-product/station/page" :option="option" :page="page" :query="query" file-name="工位配置"> </export-comp>
  </div>
</template>

<script>
import { getList, getDetail, add, update, remove } from '@/api/product/station';
import { getList as getProductionlineList, checkLine } from '@/api/product/productionline';
import { mapGetters } from 'vuex';
import exportComp from '@/components/export-comp';
export default {
  components: {
    exportComp
  },
  data() {
    return {
      form: {},
      data: [],
      defaults: {},
      query: {},
      loading: true,
      lineList: [],
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'calc(100vh - 413px)',
        calcHeight: 30,
        selection: true,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        indexLabel: '序号',
        addBtn: true,
        viewBtn: true,
        viewBtnText: '详情',
        delBtn: false,
        excelBtn: true,
        selection: true,
        dialogDrag: false, // 开启拖拽功能
        dialogClickModal: false,
        column: [
          {
            label: '工位名称',
            prop: 'name',
            search: true,
            span: 12,
            order: 2,
            rules: [{ required: true, message: '请输入工位名称', trigger: 'blur' }]
          },
          {
            label: '工位编码',
            prop: 'code',
            span: 12,
            order: 1,
            rules: [{ required: true, message: '请输入工位编码', trigger: 'blur' }]
          },
          {
            label: '产线名称',
            prop: 'lineId',
            type: 'select',
            popperClass: 'line-select',
            search: true,
            span: 12,
            allowCreate: true,
            filterable: true,
            order: 3,
            searchOrder: 1,
            dicData: [],
            rules: [{ required: true, message: '请选择产线名称', trigger: 'change' }]
          },
          {
            label: '产线编码',
            prop: 'lineCode',
            span: 12,
            order: 4,
            disabled: true,
            addDisplay: false,
            editDisplay: false
          },
          {
            label: '备注',
            prop: 'remark',
            type: 'textarea',
            span: 24,
            order: 11,
            maxRows: 3,
            minRows: 3,
            maxlength: 300,
            showWordLimit: true
          }
        ]
      },
      modalTitle: ''
    };
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.station_add, false),
        viewBtn: this.vaildData(this.permission.station_view, false),
        delBtn: this.vaildData(this.permission.station_delete, false),
        editBtn: this.vaildData(this.permission.station_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  mounted() {
    this.getProductionlineListData();
  },
  methods: {
    // 获取产线数据
    getProductionlineListData() {
      getProductionlineList(1, 10000)
        .then((res) => {
          if (res && res.data.code === 200) {
            if (res.data.data && res.data.data.records) {
              this.lineList = res.data.data.records.map((item) => {
                return {
                  label: item.name,
                  value: item.id,
                  code: item.code
                };
              });
              let findIndex = this.option.column.findIndex((item) => item.prop === 'lineId');
              if (findIndex > -1) {
                this.option.column[findIndex].dicData = this.lineList;
              }
            }
          }
        })
        .catch((err) => {});
    },
    rowSave(row, done, loading) {
      let lineId = row.lineId;
      let find = this.lineList.find((item) => item.value === lineId);
      row.lineName = find.label;
      row.lineCode = find.code;
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '添加工位成功!'
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '编辑工位成功!'
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    beforeOpen(done, type) {
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },

    // 重新加载公用表格弹窗组件
    newKey() {
      this.timer = new Date().getTime();
    }
  }
};
</script>
